package cn.dogger.easyboot.system.service.impl;


import cn.dogger.easyboot.common.exception.RollBackException;
import cn.dogger.easyboot.system.domain.bo.SysUserRoleBO;
import cn.dogger.easyboot.system.domain.dto.SysUserRoleDTO;
import cn.dogger.easyboot.system.domain.entity.SysUserRole;
import cn.dogger.easyboot.system.domain.vo.SysUserRoleDetail;
import cn.dogger.easyboot.system.domain.vo.SysUserRoleVO;
import cn.dogger.easyboot.system.mapper.SysUserRoleMapper;
import cn.dogger.easyboot.system.service.SysUserRoleService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
* 用户和角色关联表
* @author dogger
* 2022-04-16
*/
@Service
public class SysUserRoleServiceImpl  extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

         private final SysUserRoleMapper mapper;

         public SysUserRoleServiceImpl(SysUserRoleMapper mapper) {
            this.mapper = mapper;
         }
         
    @Override
    public List<SysUserRoleVO> page(SysUserRoleDTO dto) {
       // dto.setCreatedBy(TokenUtil.getLoginInfo().getUserId());
        return mapper.list(dto);
    }


    @Override
    @Transactional(rollbackFor = RollBackException.class)
    public void add(SysUserRoleBO bo) {
        SysUserRole entity = BeanUtil.copyProperties(bo, SysUserRole.class);
        mapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = RollBackException.class)
    public void edit(SysUserRoleBO bo) {
        SysUserRole entity = BeanUtil.copyProperties(bo, SysUserRole.class);
        mapper.updateById(entity);
    }

    @Override
    public SysUserRoleDetail detailById(String id) {
       SysUserRole entity =  mapper.selectById(id);
        return BeanUtil.toBean(entity,SysUserRoleDetail.class);
    }

    @Override
    public void removeByIds(String[] ids) {
        List<String> list = Arrays.asList(ids);
        mapper.deleteBatchIds(list);
    }
}